package com.anyou.mall.help.service.impl;

import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.order.domain.OrdOrder;
import com.anyou.mall.order.mapper.OrdOrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.help.mapper.HelpDocumentMapper;
import com.anyou.mall.help.domain.HelpDocument;
import com.anyou.mall.help.service.IHelpDocumentService;

/**
 * 帮助文档Service业务层处理
 *
 * @author xq
 * @date 2022-03-21
 */
@Service
public class HelpDocumentServiceImpl extends ServiceImpl<HelpDocumentMapper, HelpDocument> implements IHelpDocumentService
{
    @Autowired
    private HelpDocumentMapper helpDocumentMapper;

    @Override
    public ApiResult<HelpDocument> get() {
        return ApiResult.success(helpDocumentMapper.selectOne(new LambdaQueryWrapper<HelpDocument>()
                .eq(HelpDocument::getDistinguish,1)));
    }

    /**
     * 查询帮助文档
     *
     * @param docId 帮助文档主键
     * @return 帮助文档
     */
    @Override
    public HelpDocument selectHelpDocumentByDocId(Long docId)
    {
        return helpDocumentMapper.selectHelpDocumentByDocId(docId);
    }

    /**
     * 查询帮助文档列表
     *
     * @param helpDocument 帮助文档
     * @return 帮助文档
     */
    @Override
    public List<HelpDocument> selectHelpDocumentList(HelpDocument helpDocument)
    {
        List<HelpDocument> helpDocument1 = helpDocumentMapper.selectHelpDocumentList(helpDocument);

        return helpDocument1;
    }

    /**
     * 新增帮助文档
     *
     * @param helpDocument 帮助文档
     * @return 结果
     */
    @Override
    public int insertHelpDocument(HelpDocument helpDocument)
    {

        return helpDocumentMapper.insert(helpDocument);
    }

    /**
     * 修改帮助文档
     *
     * @param helpDocument 帮助文档
     * @return 结果
     */
    @Override
    public int updateHelpDocument(HelpDocument helpDocument)
    {
        helpDocumentMapper.updateById(helpDocument);
        return 1;
    }

    @Override
    public int updateAfterSalesService(HelpDocument helpDocument) {
        HelpDocument helpDocument1 = helpDocumentMapper.selectOne(new LambdaQueryWrapper<HelpDocument>().eq(HelpDocument::getDistinguish, 1));
        if (helpDocument1==null){
            helpDocumentMapper.insert(helpDocument);
        }else {
            helpDocument1.setDocContent(helpDocument.getDocContent());
            helpDocumentMapper.updateById(helpDocument1);
        }
        return 1;
    }

    /**
     * 批量删除帮助文档
     *
     * @param docIds 需要删除的帮助文档主键
     * @return 结果
     */
    @Override
    public int deleteHelpDocumentByDocIds(Long[] docIds)
    {
        return helpDocumentMapper.deleteHelpDocumentByDocIds(docIds);
    }

    /**
     * 删除帮助文档信息
     *
     * @param docId 帮助文档主键
     * @return 结果
     */
    @Override
    public int deleteHelpDocumentByDocId(Long docId)
    {
        return helpDocumentMapper.deleteById(docId);
    }

    @Override
    public ApiResult isTop(HelpDocument helpDocument) {

        return ApiResult.success(helpDocumentMapper.update(null,new LambdaUpdateWrapper<HelpDocument>()
                .set(HelpDocument::getIsTop,helpDocument.getIsTop())
                .eq(HelpDocument::getDocId,helpDocument.getDocId())));
    }
}
